package com.bitbakery.plugin.cl.lang.psi;

import com.bitbakery.plugin.cl.lang.CLElementVisitor;
import com.bitbakery.plugin.cl.lang.lexer.CLTokenTypes;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiNamedElement;
import com.intellij.util.Icons;
import com.intellij.util.IncorrectOperationException;

import javax.swing.*;

public class CLDefvar extends CLElement implements PsiNamedElement {
    public CLDefvar(final ASTNode node) {
        super(node);
    }

    public String getName() {
        final ASTNode nameIdentifier = findNameIdentifier();
        return nameIdentifier.getText();
    }

    public ASTNode findNameIdentifier() {
        return getNode().findChildByType(CLTokenTypes.SYMBOL);
    }

    public PsiElement setName(String name) throws IncorrectOperationException {
/*
        final ASTNode nameElement = JSChangeUtil.createNameIdentifier(getProject(), name);
        getNode().replaceChild(getNode().getFirstChildNode(), nameElement);
*/
        return this;
    }

    public String toString() {
        return "CLDefvar";
    }

    public void accept(PsiElementVisitor visitor) {
        if (visitor instanceof CLElementVisitor) {
            ((CLElementVisitor) visitor).visitCommonLispElement(this);
        } else {
            visitor.visitElement(this);
        }
    }

    public int getTextOffset() {
        final ASTNode name = findNameIdentifier();
        return name != null ? name.getStartOffset() : super.getTextOffset();
    }

    public Icon getIcon(int flags) {
        return Icons.VARIABLE_ICON;
    }
}
